/********************************************************************************
* @author: Liu  HZ
* @date: 2024/9/8
* @description: 获取系统核心数 / 线程数
********************************************************************************/

#ifndef THE_PATH_OF_CPP_SYSTEMINFO_HPP
#define THE_PATH_OF_CPP_SYSTEMINFO_HPP

#include <thread>
#include <fstream>
#include <sstream>

// 获取系统线程数
void getSystemThreads(uint64_t &threadCount) {
    threadCount = std::thread::hardware_concurrency();
}

// 获取系统线程数
uint64_t getSystemThreads() {
    std::ifstream cpuinfo("/proc/cpuinfo");
    uint64_t coreCount = 0, threadCount = 0;
    std::string line;
    while (std::getline(cpuinfo, line)) {
        if (line.find("processor") != std::string::npos) {
            ++threadCount;
        } else if (line.find("cpu cores") != std::string::npos) {
            std::istringstream iss(line);
            std::string token;
            while (iss >> token) {
                if (token == "cores") {
                    iss >> coreCount;
                    break;
                }
            }
        }
    }

    return threadCount;
}

// 获取系统核心数
int getSystemCoreCount() {
    std::ifstream cpuinfo("/proc/cpuinfo");
    int coreCount = 0;
    std::string line;
    while (std::getline(cpuinfo, line)) {
        if (line.find("processor") != std::string::npos) {
            ++coreCount;
        }
    }

    return coreCount;
}


#endif //THE_PATH_OF_CPP_SYSTEMINFO_HPP
